var progect_type = null;
var work_type = null;
var voltage_type = null;
$(function () {
    $.when(getdatadictionary("progect_type"), getdatadictionary("work_type"), getdatadictionary("voltage_type")).done($("#jqGrid").jqGrid({
        url: baseURL + 'api/bmaterialquantities/list?aguid=' + GetUrlParam("aguid"),
        datatype: "json",
        colModel: [
            {label: 'guid', name: 'guid', index: 'guid', width: 50, key: true, hidden: true},
            {label: '材料信息ID', name: 'mGuid', index: 'm_guid', width: 80, hidden: true},
            {
                label: '项目类别',
                name: 'mQType',
                index: 'm_q_type',
                width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    for (d in progect_type) {
                        if (cellvalue == progect_type[d].dKey) return progect_type[d].dValue;
                    }
                }
            },
            {
                label: '工作类别', name: 'mQWork', index: 'm_q_work', width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    for (d in work_type) {
                        if (cellvalue == work_type[d].dKey) return work_type[d].dValue;
                    }
                }
            },
            {label: '工程量', name: 'mQQuantities', index: 'm_q_quantities', width: 80 },
            {label: '取值说明', name: 'mQExplain', index: 'm_q_explain', width: 80},
            {label: '购置单位', name: 'mQUnit', index: 'm_q_unit', width: 80},
            {label: '计量单位', name: 'mQMeterUnit', index: 'm_q_meter_unit', width: 80},
            {label: '单价（元）', name: 'mQPrice', index: 'm_q_price', width: 80},
            {label: '计量与计价比值', name: 'mQRatio', index: 'm_q_ratio', width: 80},
            {label: '备注', name: 'mQRemarks', index: 'm_q_remarks', width: 80}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    }));
});

function getdatadictionary(sss) {
    $.get(baseURL + "api/bdatadictionary/select?d_code=" + sss, function (r) {
        if (sss == "progect_type") {
            progect_type = r.returnlist;
        } else if (sss == "work_type") {
            work_type = r.returnlist;
        }
        else if  (sss == "voltage_type") {
            voltage_type = r.returnlist;
        }
        vm.work_type = work_type;
        vm.progect_type = progect_type;
    });
}
function getworktype(txt, v) {
    if (v == 1) {
        for (d in work_type) {
            if (work_type[d].dKey == txt) return work_type[d].dValue;
        }
    }else if (v == 2) {
        for (d in voltage_type) {
            if (voltage_type[d].dKey == txt) return voltage_type[d].dValue;
        }
    } else return "";


}


var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            mQWork: null,
            mQType: null
        },
        showList: true,
        title: null,
        mGuid: GetUrlParam("aguid"),
        bMaterialQuantities: {},
        progect_type: progect_type,
        work_type: work_type
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";

            vm.bMaterialQuantities = {mGuid: GetUrlParam("aguid")};

            vm.progect_type = progect_type;
            vm.work_type = work_type;
            vm.getDept(GetUrlParam("aguid"));
        },
        update: function (event) {
            var guid = getSelectedRow();
            if (guid == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.mGuid = GetUrlParam("aguid");
            vm.progect_type = progect_type;
            vm.work_type = work_type;
            vm.getInfo(guid)
        },
        saveOrUpdate: function (event) {
            var url = vm.bMaterialQuantities.guid == null ? "api/bmaterialquantities/save" : "api/bmaterialquantities/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.bMaterialQuantities),
                success: function (r) {
                    if (r.code === 0) {
                        if(vm.bMaterialQuantities.guid == null)
                        {

                            confirmaa('操作成功，是否继续添加？', function (r) {

                                if(r=="ok")
                                {

                                    vm.add();
                                }
                                if(r=="no")
                                {
                                    vm.reload();
                                }
                            });

                        }
                        else
                        {
                            alert('操作成功', function (index) {

                                vm.reload();
                            });
                        }
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var guids = getSelectedRows();
            if (guids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "api/bmaterialquantities/delete",
                    contentType: "application/json",
                    data: JSON.stringify(guids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (guid) {
            $.get(baseURL + "api/bmaterialquantities/info/" + guid, function (r) {
                vm.bMaterialQuantities = r.bMaterialQuantities;
                vm.bMaterialQuantities.mGuid = GetUrlParam("aguid");
            });
        },

        addall:function(){

            confirm('确定要添加全部工程量？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "api/bmaterialquantities/addall",
                    contentType: "application/json",
                    data: JSON.stringify( GetUrlParam("aguid")),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },


        getDept: function (guid) {
            //加载部门树
            $("#jqGrid1").jqGrid({
                url: baseURL + 'api/bbudgetitem/list?dQuery=' + GetUrlParam("mQuery") + "&dClass="
                + GetUrlParam("mClass") + "&dItem=" + GetUrlParam("mItem"),
                datatype: "json",
                colModel: [
                    {label: 'guid', name: 'guid', index: 'guid', width: 50, key: true, hidden: true},
                    {label: '所属预算标准ID', name: 'bGuid', index: 'b_guid', width: 80, hidden: true},
                    {
                        label: '项目类别',
                        name: 'bIType',
                        index: 'b_i_type',
                        width: 80,
                        formatter: function (cellvalue, options, rowObject) {
                            var names = "";

                            for (d in progect_type) {
                                if (cellvalue == progect_type[d].dKey) {
                                    names = progect_type[d].dValue;
                                }
                            }


                            return names;
                        }
                    },
                    {label: '预算项目分类', name: 'bIClass', index: 'b_i_class', width: 80},
                    {label: '预算项目', name: 'bIItem', index: 'b_i_item', width: 80},

                    {
                        label: '工作类容',
                        name: 'bIWork',
                        index: 'b_i_work',
                        width: 80,
                        formatter: function (cellvalue, options, rowObject) {
                            var names = "";

                            for (d in work_type) {
                                if (cellvalue == work_type[d].dKey) {
                                    names = work_type[d].dValue;
                                }
                            }


                            return names;
                        }
                    },
                    {
                        label: '电压等级',
                        name: 'bIVoltage',
                        index: 'b_i_voltage',
                        width: 80, formatter: function (cellvalue, options, rowObject) {
                        for (d in voltage_type) {
                            if (cellvalue == voltage_type[d].dKey) return voltage_type[d].dValue;
                        }
                        return "";
                    }
                    },
                    {label: '预算参数', name: 'bIParameter', index: 'b_i_parameter', width: 80},
                    {label: '计价单位', name: 'bIUnit', index: 'b_i_unit', width: 80},
                    {label: '取值下限', name: 'bILower', index: 'b_i_lower', width: 80},
                    {label: '取值上限', name: 'bIHigh', index: 'b_i_high', width: 80},
                    {label: '价格', name: 'bIPrice', index: 'b_i_price', width: 80},
                    {label: '取值方式', name: 'bIValue', index: 'b_i_value', width: 80},
                    {label: '说明', name: 'bIExplain', index: 'b_i_explain', width: 80}
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList: [10, 30, 50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth: true,
                multiselect: true,
                pager: "#jqGridPager1",
                jsonReader: {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames: {
                    page: "page",
                    rows: "limit",
                    order: "order"
                },
                gridComplete: function () {
                    //隐藏grid底部滚动条
                    $("#jqGrid1").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
                }
            })
        },
        deptTree: function () {
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择预算项",
                area: ['700px', '500px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    console.log(index)
                    var grid = $("#jqGrid1");
                    var rowKey = grid.getGridParam("selrow");
                    if (!rowKey) {
                        alert("请选择一条记录");
                        return;
                    }

                    var guids = grid.getGridParam("selarrrow");
                    console.log(guids)
                    if (guids.length > 1) {
                        alert("只能选择一条记录");
                        return;
                    }

                    $.get(baseURL + "api/bbudgetitem/info/" + guids[0], function (r) {
                        var qzsm="";

                        if (r.bBudgetItem.bIVoltage!=null&&r.bBudgetItem.bIVoltage!="")
                        {
                            qzsm+="电压等级:"+ getworktype(r.bBudgetItem.bIVoltage, 2)+"   ";
                        }
                        if( r.bBudgetItem.bIParameter!=null&&r.bBudgetItem.bIParameter!="")
                        {
                            qzsm += r.bBudgetItem.bIParameter + ":" + GetUrlParam("mQuery")+"  ";
                        }
                        vm.bMaterialQuantities = {
                            mQRatio: 1,
                            mGuid: GetUrlParam("aguid"),
                            mQType: r.bBudgetItem.bIType,
                            mQWork: r.bBudgetItem.bIWork,
                            mQPrice: r.bBudgetItem.bIPrice,
                            mQMeterUnit: r.bBudgetItem.bIUnit,
                            mQQuantities: r.bBudgetItem.bIClass+"-"+ r.bBudgetItem.bIItem +"-"+ getworktype(r.bBudgetItem.bIWork, 1),
                            mQUnit: GetUrlParam("mUnit"),
                            mQExplain:  qzsm
                        };
                    });
                    layer.close(index);
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'mQWork': vm.q.mQWork,
                    'mQType': vm.q.mQType
                },
                page: page
            }).trigger("reloadGrid");
        }
    }
});